package com.tim.temp;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.apache.commons.collections4.MapUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class ZrGps {


    String host="http://gatewayapi.lunztech.cn";
    String appKey="2C6074D0-C8E4-4BA4-A50A-E3A46EAD394A";
    String appCode="appCode"+System.currentTimeMillis();

    public static void main(String[] args) throws IOException {
        ZrGps zrGps=new ZrGps();
        zrGps.install();
        zrGps.query();
//        zrGps.query("APP202400125");
//        zrGps.queryPost("appCode1755653239451");
    }

    private  void install() throws IOException {
        String path="/api/ApiPlat/AddOrderWithVpsInfos";
        String json = "{\n" +
                "  \"appCode\": \""+appCode+"\",\n" +
                "  \"ProductId\": \"a01135d4-92c1-1dba-d27c-2dc4b1c82dbc\",\n" +
                "  \"DistrictCode\": \"110102\",\n" +
                "  \"InstallAdd\": \"北京市朝阳区XX路XX号\",\n" +
                "  \"InstallTime\": \"2025-08-19 14:30\",\n" +
                "  \"ShopName\": \"中瑞安装店\",\n" +
                "  \"LinkMan\": \"张伟\",\n" +
                "  \"LinkPhone\": \"13800138000\",\n" +
                "  \"UserName\": \"李强\",\n" +
                "  \"UserPhone\": \"13900139000\",\n" +
                "  \"VIN\": \"LSVCC2CBXBM123456\"\n" +
                "}\n";

        InstallOrderRequest req = JSON.parseObject(json, InstallOrderRequest.class);
        postZr(path, req);
    }

    private void query() throws IOException {
        String path="/api/ApiPlat/GetBaseOrderInfoByAppCode";
        Map<String,String> reqParamMap=new HashMap<>();
        reqParamMap.put("appcode",appCode);

        getZr(path, reqParamMap);
    }

    private void query(String appCode) throws IOException {
        this.appCode=appCode;
        query();
    }

    private  void queryPost(String appCode) throws IOException {
        String path="/api/ApiPlat/GetBaseOrderInfoByAppCode";
        String json = "{\n" +
                "  \"appCode\": \""+appCode+"\"\n" +
                "}\n";

        QueryOrderRequest req = JSON.parseObject(json, QueryOrderRequest.class);
        postZr(path, req);
    }

    private  void getZr(String path, Map<String,String> reqParamMap) throws IOException {
        String url=host + path;
        if(MapUtils.isNotEmpty(reqParamMap)){
            String params = reqParamMap.keySet().stream()
                    .map(k -> k + "=" + reqParamMap.get(k))
                    .collect(Collectors.joining("&"));
            url += "?"+params;
        }
        // 构建请求
        Request.Builder builder = new Request.Builder()
                .url(url)
                .header("appKey", appKey);
        builder=builder.get();
        Request request = builder.build();

        // 创建 OkHttpClient
        OkHttpClient client = new OkHttpClient();
        // 发送请求
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                String responseBody = response.body().string();
                System.out.println(responseBody);
                System.out.println(JSON.parseObject(responseBody, QueryOrderDetailResponse.class));
            } else {
                System.err.println("请求失败: " + response.code());
            }
        }
    }

    private  void postZr(String path, Object param) throws IOException {
        // 构建请求
        Request.Builder builder = new Request.Builder()
                .url(host + path)
                .header("appKey", appKey);
        builder=builder.post(RequestBody.create(MediaType.get("application/json"), JSON.toJSONString(param)));
        Request request = builder.build();

        // 创建 OkHttpClient
        OkHttpClient client = new OkHttpClient();
        // 发送请求
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                String responseBody = response.body().string();
                System.out.println(JSON.parseObject(responseBody, InstallOrderResponse.class));
            } else {
                System.err.println("请求失败: " + response.code());
            }
        }
    }
}
